﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SedCup.Code.View.Forms;
using SedCup.Code.Model;
using SedCup.Code.View.Components;
using SedCup.Code.Control;

namespace SedCup.Code.View.Forms
{
    public partial class MainWindow : Form
    {
        private MainController mainController;

        private ServerResponse currentResponse;

        public ServerResponse CurrentResponse
        {
            get { return currentResponse; }
            set { currentResponse = value; this.DrawWindow(); }
        }


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="mainController"></param>
        public MainWindow(MainController mainController)
        {
            this.mainController = mainController;

            InitializeComponent();

            this.BackColor = Settings.GUI_BACKCOLOR;
            this.flowLayoutPanelRight.BackColor = Settings.GUI_BACKCOLOR;
            this.flowLayoutPanelTop.BackColor = Settings.GUI_BACKCOLOR; ;

            if (Settings.DEMO)
            {
                this.UpdateGame(Responder.GetResponse());
            }
        }

        /// <summary>
        /// Lekéri a friss adatokat és kirajzolja az ablakot
        /// </summary>
        public void UpdateGame(ServerResponse response)
        {
            // TODO: MainWindow.UpdateGame
            this.CurrentResponse = response;
            //this.CurrentResponse = ; // ez elvégzi a frissítést is   
        }

        /// <summary>
        /// Kirajzolja az ablakot
        /// </summary>
        private void DrawWindow()
        {
            this.DrawMap();
            this.DrawStats();
            this.DrawTops();
        }

        /// <summary>
        /// Legjobb és legrosszabb dolgozó kiírása
        /// </summary>
        private void DrawTops()
        {
            // TODO: DrawTops
            this.flowLayoutPanelRight.Controls.Clear();
        }

        /// <summary>
        /// Statisztikák kiírása
        /// </summary>
        private void DrawStats()
        {
            // TODO: DrawStats
            this.labelScore.Text = this.CurrentResponse.money + " fb";
            this.labelTime.Text = this.CurrentResponse.secondsLeft + " s (" 
                + this.CurrentResponse.turn + " / 100 kör)";
            //this.labelTasks.Text = "5 / 13";
        }

        /// <summary>
        /// Pálya kirajzolása
        /// </summary>
        private void DrawMap()
        {
            this.panelGame.Controls.Clear();

            int xcoord = 0, ycoord = 0;
            int n = (int)Math.Sqrt(this.CurrentResponse.mapSize);
            int bound = n * 2 - 1;
            int index = 1;

            for (int i = 0; i < bound; i++) // függőleges (y)
            {
                for (int j = 0; j < bound; j++) // vízszintes (x)
                {
                    // páratlan helyken út
                    if (i % 2 != 0 || j % 2 != 0)
                    {
                        Road road;

                        if (this.blockedAt(i, j, n))
                        {
                            road = new Road(Road.Content.NOTHING);
                        }
                        else
                        {
                            if (j % 2 != 0)
                                road = new Road(Road.Content.H_ROAD);
                            else
                                road = new Road(Road.Content.V_ROAD);
                        }

                        road.Location = new Point(xcoord, ycoord);

                        xcoord += Settings.FIELDSIZE;

                        this.panelGame.Controls.Add(road);
                    }
                    else
                    {
                        Field field = null;

                        foreach (var item in this.CurrentResponse.headQuarters)
                        {
                            if (item.coordinate == index)
                            {
                                field = new FieldHQ(index, item);
                                // goto >>
                                goto Paint;
                            }
                        }

                        foreach (var item in this.CurrentResponse.tasks)
                        {
                            if (item.coordinate == index)
                            {
                                field = new FieldTask(index, item);
                                // goto >>
                                goto Paint;
                            }
                        }

                        field = new FieldEmpty(index);

                        // >> goto
                    Paint:
                        List<Agent> fieldAgents = new List<Agent>();

                        foreach (var item in this.CurrentResponse.agents)
                        {
                            if (item.coordinate == index)
                            {
                                fieldAgents.Add(item);
                            }
                        }

                        field.Agents = fieldAgents;

                        field.Location = new Point(xcoord, ycoord);

                        xcoord += Settings.FIELDSIZE;

                        this.panelGame.Controls.Add(field);

                    index++;
                    }
                } /* end of for (int j = 0; j < bound; j++) */

                xcoord = 0;
                ycoord += Settings.FIELDSIZE;
            }
        }

        /// <summary>
        /// Igazat ad, ha az adott helyen nincs út
        /// </summary>
        /// <returns></returns>
        private bool blockedAt(int i, int j, int n)
        {
            if (i % 2 != 0 && j % 2 != 0)
            {
                return true;
            }

            bool ret = false;

            // TODO: test
            if (i % 2 != 0) // függőleges út
            {
                int top = (i / 2) * n + j / 2 + 1;
                int bottom = (i / 2 + 1) * n + j / 2 + 1;

                ret |= this.CurrentResponse.roadBlocks.ContainsKey(top)
                        && this.CurrentResponse.roadBlocks[top].Contains(bottom);
            }
            else
            {
                int left = (i / 2) * n + j / 2 + 1; // korábbi sorok + baloldali mező offset
                int right = (i / 2) * n + j / 2 + 2; // korábbi sorok + jobboldali mező offset + indexelés-korrekció

                ret |= this.CurrentResponse.roadBlocks.ContainsKey(left)
                        && this.CurrentResponse.roadBlocks[left].Contains(right);
            }

            return ret;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox.ShowAbout();
        }
    }
}
